#include "StdAfx.h"
#include "UserMapper.h"

namespace DBLayer{

	CUserMapper::CUserMapper(void)
	{
	}

	CUserMapper::~CUserMapper(void)
	{
	}

	bool CUserMapper::loadData(int type,CTinyDBInterface* DBIF)
	{
		m_DBType = type;
		/*mysql*/
		m_DBIF = DBIF;
		m_MapperData.clear();
		if(type == DBTYPE_MYSQL)
		{
			try{
				sql::ResultSet * rs = (sql::ResultSet*)DBIF->executeSQL(std::string("SELECT UserName, PassWord, GroupID FROM userinfo")+m_filterStr );
				if(rs == NULL)
					return false;
				while (rs->next())
				{
					//process data
					std::string username = rs->getString("UserName");
					std::string password = rs->getString("PassWord");
					std::string groupid = rs->getString("GroupID");
					std::map<std::string,std::string> tempMap;
					tempMap.insert(std::pair<std::string,std::string>("UserName",username));
					tempMap.insert(std::pair<std::string,std::string>("PassWord",password));
					tempMap.insert(std::pair<std::string,std::string>("GroupID",groupid));
					m_MapperData.push_back(tempMap);
				}
			}
			catch (sql::SQLException &e) {
				std::string err =  e.what();
				return false;
			}
		}

		/*MS SQL
		ORACLE
		...*/

		return true;
	}

	std::string CUserMapper::getPassWordByName(std::string name)
	{
		for(int i =0;i<m_MapperData.size();++i)
		{
			std::map<std::string,std::string>::iterator it = m_MapperData[i].find("UserName");
			if(it != m_MapperData[i].end()&&it->second == name)
			{
				std::map<std::string,std::string>::iterator it2 = m_MapperData[i].find("PassWord");
				if(it2 != m_MapperData[i].end())
					return it2->second;
			}
		}
		return std::string();
	}

	std::string CUserMapper::getGroupIDByName(std::string name)
	{
		for(int i =0;i<m_MapperData.size();++i)
		{
			std::map<std::string,std::string>::iterator it = m_MapperData[i].find("UserName");
			if(it != m_MapperData[i].end()&&it->second == name)
			{
				std::map<std::string,std::string>::iterator it2 = m_MapperData[i].find("GroupID");
				if(it2 != m_MapperData[i].end())
					return it2->second;
			}
		}
		return std::string();
	}

	std::vector<std::string> CUserMapper::getFriends(std::string username)
	{
		//get group id from inner data;

		std::string groupids = getGroupIDByName(username);
		std::string groupid;

		std::vector<std::string> userVector;

		/*mysql*/

		if(m_DBType == DBTYPE_MYSQL)
		{
			try{
				while(groupids != "")
				{
					if(groupids.find(",") == std::string::npos)
					{
						groupid = groupids;
						groupids = "";
					}
					else
					{
						groupid = groupids.substr(0,groupids.find(","));
						groupids = groupids.substr(groupids.find(",")+1);
					}
					sql::ResultSet * rs = (sql::ResultSet*)m_DBIF->executeSQL(std::string("SELECT UserName FROM userinfo where GroupID = '"+groupid+"'"));
					if(rs == NULL)
						return std::vector<std::string>();
					while (rs->next())
					{
						//process data
						std::string username = rs->getString("UserName");
						userVector.push_back(username+":"+groupid);
					}
				}
			}
			catch (sql::SQLException &e) {
				std::string err =  e.what();
				return std::vector<std::string>();
			}
		}

		/*MS SQL
		ORACLE
		...*/
		return userVector;
	}
}
